<script setup>
import { ref, onMounted } from 'vue'
import { category,addApi, deleteApi, queryByIdApi ,updateApi } from '@/api/classmanage'
import { ElMessage } from 'element-plus'
const dialogVisible = ref(false)
const title = ref('商品类别设置')
const formData = ref({
  name: '',
  parentId: ''
})
const emits = defineEmits(['success'])
const parentOptions = ref([])
const handleSubmit = async () => {
  let result;
  if (formData.value.id) { 
    result = await updateApi(formData.value)
  } else { 
    result = await addApi(formData.value)
  }
  if (result.code) { 
    ElMessage.success('操作成功')
    dialogVisible.value = false
   
    emits('success')
  } else { 
    ElMessage.error('操作失败')
  }
}
defineExpose({
  // 暴露参数
  dialogVisible,
  title,
  formData,
  parentOptions,
  handleSubmit
  
})
</script>
<template>
   <el-dialog v-model="dialogVisible" :title="title" width="500px">
      <el-form :model="formData" ref="formRef" label-width="100px">
        <el-form-item label="商品类别" prop="name" :rules="[
          { required: true, message: '请填写商品类别!', trigger: 'blur' }
        ]">
          <el-input v-model="formData.name" placeholder="请输入商品类别名称" />
        </el-form-item>

        <el-form-item label="所属类别" prop="parentId">
          <el-select v-model="formData.parentId" placeholder="请选择所属类别">
            <el-option value="0" label="顶级类别" />
            <!-- 动态生成父级类别选项 -->
            <template v-for="item in parentOptions" :key="item.id">
              <el-option :value="item.id" :label="item.name" />
            </template>
          </el-select>
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">关闭</el-button>
        <el-button type="primary" @click="handleSubmit">
          提交
        </el-button>
      </template>
    </el-dialog>
</template>